from sqlalchemy import Column, String, Integer, DateTime, Boolean
from sqlalchemy.dialects.postgresql import ARRAY
from sqlalchemy.orm import relationship
from .archivable import Archivable
from app.db.base_class import Base
from enum import Enum


# 触发类型枚举
class TriggerType(str, Enum):
    TIMED_ALERT = "定时预警"
    ROUTINE_NOTIFICATION = "日常通报"


# 播报对象枚举
class BroadcastTargetCategory(str, Enum):
    SELECTED_PERSONNEL = "选定人员"
    CORRESPONDING_RESPONSIBLE_PERSON = "对应负责人"


# 播报内容枚举
class BroadcastContentCategory(str, Enum):
    DEFAULT_COPY = "预设文案"
    NEW_COPY = "新建文案"


# 优先级枚举
class Priority(str, Enum):
    HIGH = "最高"
    MEDIUM = "中等"
    LOW = "最低"


# 语速枚举
class Speed(str, Enum):
    SLOW = "慢"
    MEDIUM = "中等"
    FAST = "快"


class BroadcastTask(Base, Archivable):
    __tablename__ = "broadcast_task"

    name = Column(String, nullable=False, comment="任务名称")
    category = Column(String, nullable=False, comment="播报分类")
    description = Column(String, nullable=False, comment="任务简介")
    trigger_type = Column(String, default=TriggerType.TIMED_ALERT, nullable=False, comment="触发类型")
    warning_time = Column(DateTime, comment="定时预警时间")
    priority = Column(String, default=Priority.MEDIUM, comment="优先级")
    monitor_data = Column(String, comment="监控数据")
    condition = Column(String, comment="判断条件")
    broadcast_target_category = Column(String, default=BroadcastTargetCategory.SELECTED_PERSONNEL,
                                       comment="播报对象的分类")
    broadcast_target_ids = Column(ARRAY(String), nullable=True, comment="播报对象用户ID列表")  # 使用 ARRAY 类型
    broadcast_content_ids = Column(ARRAY(String), nullable=True, comment="播报内容ID列表")
    broadcast_content_category = Column(String, default=BroadcastContentCategory.DEFAULT_COPY,
                                        comment="播报内容的分类")
    broadcast_content = Column(String, nullable=True, comment="播报内容")
    speed = Column(String, default=Speed.MEDIUM, nullable=False, comment="语速")
    repeat_frequency = Column(Integer, nullable=False, comment="重复频率")
    auto_read = Column(Boolean, nullable=False, comment="自动朗读")
    audio_url = Column(String, nullable=True, comment="音频文件URL")
    broadcast_log = relationship("BroadcastLog", backref="broadcast_task", lazy="dynamic", cascade="all, delete")


# 播报对象用户表：后期对接真实数据，目前为了测试方便，先建一张broadcast_target表
class BroadcastTarget(Base, Archivable):
    __tablename__ = "broadcast_target"
    name = Column(String, nullable=False, comment="播报对象名称")
    department = Column(String, nullable=False, comment="所属部门")
